from django.test import TestCase
import json
import yaml
import base64
from layout_image.views import build_yml
import collections

# Create your tests here.


if __name__ == '__main__':
    topo_data = {'nodes': [{'name': 'Container', 'type': 'Container', 'id': '63yorm3qmro0', 'x': 140, 'y': 120, 'icon': '', 'width': 200, 'height': 120, 'initW': 200, 'initH': 120, 'classType': 'T1', 'isLeftConnectShow': False, 'isRightConnectShow': True, 'containNodes': [], 'attrs': {'id': 'f009a105-44f7-4111-8e5a-cecc556515af', 'name': 'redis:3.2-alpine', 'desc': 'redis', 'port': '6379', 'open': True, 'vul_name': 'redis', 'raw': {'image_id': 'f009a105-44f7-4111-8e5a-cecc556515af', 'status': {'status': '', 'is_check': False, 'container_id': '', 'start_date': '', 'end_date': '', 'host': '', 'port': '', 'progress': 0, 'progress_status': '', 'task_id': '', 'now': 1607486074}, 'image_name': 'redis:3.2-alpine', 'image_vul_name': 'redis', 'image_port': '6379', 'image_desc': 'redis', 'rank': 2.5, 'is_ok': True, 'is_share': False, 'create_date': '2020-12-07T16:45:17.387204', 'update_date': '2020-12-07T16:45:17.420568'}}, 'isSelect': False}, {'name': 'Network', 'type': 'Network', 'id': '1860gad1nlpc', 'x': 440, 'y': 200, 'icon': '', 'width': 200, 'height': 100, 'initW': 200, 'initH': 100, 'classType': 'T1', 'isLeftConnectShow': True, 'isRightConnectShow': False, 'containNodes': [], 'attrs': {'id': 'd9f9a0fb-c0d0-4831-9821-9947028c5a73', 'name': 'network-002', 'subnet': '172.13.2.0/24', 'gateway': '172.13.2.1', 'raw': {'net_work_id': 'd9f9a0fb-c0d0-4831-9821-9947028c5a73', 'net_work_client_id': '0489dcf918d39034bd39b96c6cb33f596a8cfc2262791d657d9974a6ffc20be2', 'create_user': 1, 'net_work_name': 'network-002', 'net_work_subnet': '172.13.2.0/24', 'net_work_gateway': '172.13.2.1', 'net_work_scope': 'local', 'net_work_driver': 'bridge', 'enable_ipv6': False, 'create_date': '2020-12-08T16:29:10.312053', 'update_date': '2020-12-08T16:29:10.312070'}}, 'isSelect': False}, {'name': 'Container', 'type': 'Container', 'id': 'gargmjrhv2o', 'x': 140, 'y': 280, 'icon': '', 'width': 200, 'height': 120, 'initW': 200, 'initH': 120, 'classType': 'T1', 'isLeftConnectShow': False, 'isRightConnectShow': True, 'containNodes': [], 'attrs': {'id': 'f009a105-44f7-4111-8e5a-cecc556515af', 'name': 'redis:3.2-alpine', 'desc': 'redis', 'port': '6379', 'open': False, 'vul_name': 'redis', 'raw': {'image_id': 'f009a105-44f7-4111-8e5a-cecc556515af', 'status': {'status': '', 'is_check': False, 'container_id': '', 'start_date': '', 'end_date': '', 'host': '', 'port': '', 'progress': 0, 'progress_status': '', 'task_id': '', 'now': 1607486081}, 'image_name': 'redis:3.2-alpine', 'image_vul_name': 'redis', 'image_port': '6379', 'image_desc': 'redis', 'rank': 2.5, 'is_ok': True, 'is_share': False, 'create_date': '2020-12-07T16:45:17.387204', 'update_date': '2020-12-07T16:45:17.420568'}}, 'isSelect': True}], 'connectors': [{'id': 'm9o84w8vp1', 'type': 'Line', 'strokeW': 3, 'color': '#768699', 'targetNode': {'x': 440, 'y': 200, 'id': '1860gad1nlpc', 'width': 200, 'height': 100}, 'sourceNode': {'x': 140, 'y': 120, 'id': '63yorm3qmro0', 'width': 200, 'height': 120}, 'isSelect': False}, {'id': '1ebww27fghe', 'type': 'Line', 'strokeW': 3, 'color': '#768699', 'targetNode': {'x': 440, 'y': 200, 'id': '1860gad1nlpc', 'width': 200, 'height': 100}, 'sourceNode': {'x': 140, 'y': 280, 'id': 'gargmjrhv2o', 'width': 200, 'height': 120}}]}
    topo_data = {'nodes': [{'name': 'Container', 'type': 'Container', 'id': '63yorm3qmro0', 'x': 140, 'y': 120, 'icon': '', 'width': 200, 'height': 120, 'initW': 200, 'initH': 120, 'classType': 'T1', 'isLeftConnectShow': False, 'isRightConnectShow': True, 'containNodes': [], 'attrs': {'id': 'f009a105-44f7-4111-8e5a-cecc556515af', 'name': 'redis:3.2-alpine', 'desc': 'redis', 'port': '6379', 'open': True, 'vul_name': 'redis', 'raw': {'image_id': 'f009a105-44f7-4111-8e5a-cecc556515af', 'status': {'status': '', 'is_check': False, 'container_id': '', 'start_date': '', 'end_date': '', 'host': '', 'port': '', 'progress': 0, 'progress_status': '', 'task_id': '', 'now': 1607486074}, 'image_name': 'redis:3.2-alpine', 'image_vul_name': 'redis', 'image_port': '6379', 'image_desc': 'redis', 'rank': 2.5, 'is_ok': True, 'is_share': False, 'create_date': '2020-12-07T16:45:17.387204', 'update_date': '2020-12-07T16:45:17.420568'}}, 'isSelect': False}, {'name': 'Network', 'type': 'Network', 'id': '1860gad1nlpc', 'x': 440, 'y': 200, 'icon': '', 'width': 200, 'height': 100, 'initW': 200, 'initH': 100, 'classType': 'T1', 'isLeftConnectShow': True, 'isRightConnectShow': False, 'containNodes': [], 'attrs': {'id': 'd9f9a0fb-c0d0-4831-9821-9947028c5a73', 'name': 'network-002', 'subnet': '172.13.2.0/24', 'gateway': '172.13.2.1', 'raw': {'net_work_id': 'd9f9a0fb-c0d0-4831-9821-9947028c5a73', 'net_work_client_id': '0489dcf918d39034bd39b96c6cb33f596a8cfc2262791d657d9974a6ffc20be2', 'create_user': 1, 'net_work_name': 'network-002', 'net_work_subnet': '172.13.2.0/24', 'net_work_gateway': '172.13.2.1', 'net_work_scope': 'local', 'net_work_driver': 'bridge', 'enable_ipv6': False, 'create_date': '2020-12-08T16:29:10.312053', 'update_date': '2020-12-08T16:29:10.312070'}}, 'isSelect': False}, {'name': 'Container', 'type': 'Container', 'id': 'gargmjrhv2o', 'x': 120, 'y': 260, 'icon': '', 'width': 200, 'height': 120, 'initW': 200, 'initH': 120, 'classType': 'T1', 'isLeftConnectShow': False, 'isRightConnectShow': True, 'containNodes': [], 'attrs': {'id': 'f009a105-44f7-4111-8e5a-cecc556515af', 'name': 'redis:3.2-alpine', 'desc': 'redis', 'port': '6379', 'open': False, 'vul_name': 'redis', 'raw': {'image_id': 'f009a105-44f7-4111-8e5a-cecc556515af', 'status': {'status': '', 'is_check': False, 'container_id': '', 'start_date': '', 'end_date': '', 'host': '', 'port': '', 'progress': 0, 'progress_status': '', 'task_id': '', 'now': 1607486081}, 'image_name': 'redis:3.2-alpine', 'image_vul_name': 'redis', 'image_port': '6379', 'image_desc': 'redis', 'rank': 2.5, 'is_ok': True, 'is_share': False, 'create_date': '2020-12-07T16:45:17.387204', 'update_date': '2020-12-07T16:45:17.420568'}}, 'isSelect': False}, {'name': 'Container', 'type': 'Container', 'id': '1m8wl7vwt98g', 'x': 120, 'y': 400, 'icon': '', 'width': 200, 'height': 120, 'initW': 200, 'initH': 120, 'classType': 'T1', 'isLeftConnectShow': False, 'isRightConnectShow': True, 'containNodes': [], 'attrs': {'id': 'c617f791-cac0-4b6f-974a-7843d52f84c8', 'name': 'hello-node_hello:latest', 'desc': 'hello-node_hello', 'port': '1337', 'open': False, 'vul_name': 'hello-node_hello', 'raw': {'image_id': 'c617f791-cac0-4b6f-974a-7843d52f84c8', 'status': {'status': '', 'is_check': False, 'container_id': '', 'start_date': '', 'end_date': '', 'host': '', 'port': '', 'progress': 0, 'progress_status': '', 'task_id': '', 'now': 1607508536}, 'image_name': 'hello-node_hello:latest', 'image_vul_name': 'hello-node_hello', 'image_port': '1337', 'image_desc': 'hello-node_hello', 'rank': 2.5, 'is_ok': True, 'is_share': False, 'create_date': '2020-12-07T16:45:17.126458', 'update_date': '2020-12-07T16:45:17.226729'}}, 'isSelect': False}, {'name': 'Network', 'type': 'Network', 'id': '2cwmtjqc00w0', 'x': 440, 'y': 380, 'icon': '', 'width': 200, 'height': 100, 'initW': 200, 'initH': 100, 'classType': 'T1', 'isLeftConnectShow': True, 'isRightConnectShow': False, 'containNodes': [], 'attrs': {'id': '258cf8f4-3dda-49ab-a3e8-39f4d2c9406a', 'name': 'network-003', 'subnet': '172.13.3.0/24', 'gateway': '172.13.3.1', 'raw': {'net_work_id': '258cf8f4-3dda-49ab-a3e8-39f4d2c9406a', 'net_work_client_id': '35d01da07ab2c4a837892c8a24420fd4cc1a3b6d5905661d7b5d3b1964608a7f', 'create_user': 1, 'net_work_name': 'network-003', 'net_work_subnet': '172.13.3.0/24', 'net_work_gateway': '172.13.3.1', 'net_work_scope': 'local', 'net_work_driver': 'bridge', 'enable_ipv6': False, 'create_date': '2020-12-08T16:29:26.076173', 'update_date': '2020-12-08T16:29:26.076202'}}, 'isSelect': False}], 'connectors': [{'id': 'm9o84w8vp1', 'type': 'Line', 'strokeW': 3, 'color': '#768699', 'targetNode': {'x': 440, 'y': 200, 'id': '1860gad1nlpc', 'width': 200, 'height': 100}, 'sourceNode': {'x': 140, 'y': 120, 'id': '63yorm3qmro0', 'width': 200, 'height': 120}, 'isSelect': False}, {'id': '1ebww27fghe', 'type': 'Line', 'strokeW': 3, 'color': '#768699', 'targetNode': {'x': 440, 'y': 200, 'id': '1860gad1nlpc', 'width': 200, 'height': 100}, 'sourceNode': {'x': 120, 'y': 260, 'id': 'gargmjrhv2o', 'width': 200, 'height': 120}, 'isSelect': False}, {'id': 'ix5iaw5jnm', 'type': 'Line', 'strokeW': 3, 'color': '#768699', 'targetNode': {'x': 440, 'y': 380, 'id': '2cwmtjqc00w0', 'width': 200, 'height': 100}, 'sourceNode': {'x': 120, 'y': 260, 'id': 'gargmjrhv2o', 'width': 200, 'height': 120}, 'isSelect': False}, {'id': '2paqjtj1nm4', 'type': 'Line', 'strokeW': 3, 'color': '#768699', 'targetNode': {'x': 440, 'y': 380, 'id': '2cwmtjqc00w0', 'width': 200, 'height': 100}, 'sourceNode': {'x': 120, 'y': 400, 'id': '1m8wl7vwt98g', 'width': 200, 'height': 120}, 'isSelect': False}]}

    # topo_data = json.loads(data)
    # print(json.dumps(topo_data,ensure_ascii=False,indent=4))
    connector_list = topo_data["connectors"]
    nodes = topo_data["nodes"]
    container_list = []
    network_dict = {}
    for node in nodes:
        node_type = node["type"]
        node_attrs = node["attrs"]
        node_id = node["id"]
        if node_type == "Container":
            node_open = node_attrs["open"]
            if node_open:
                check_open = True
            container_list.append(node)
        elif node_type == "Network":
            network_dict[node_id] = node
            # network_list.append(node)
    yml_data = build_yml(container_list=container_list, network_dict=network_dict, connector_list=connector_list)
    print(yaml.dump(yml_data["content"]))
